/**
 * @description 附件上传 配置
 * @author aaron
 */
import bytes from '@bixiao/utils/lib/bytes';

export default function (editor) {
    const server = editor.config.uploadAttachmentServer || '/upload'
    let maxSize = editor.config.uploadAttachmentMaxSize || '100MB'
    const minSize = editor.config.uploadAttachmentMinSize || 1
    // 默认为 100 张，需要限制可自己配置。
    const maxLength = editor.config.uploadAttachmentMaxLength || 100
    const minLength = editor.config.uploadAttachmentMinLength || 1
    // 上传图片时可自定义传递一些参数，例如传递验证的 token 等。参数会被添加到 formData 中，一起上传到服务端。
    const params = editor.config.uploadAttachmentParams || {}
    const headers = editor.config.uploadAttachmentHeaders || {}
    const fileName = editor.config.uploadAttachmentFileName || 'file'
    const allowedFileTypes = editor.config.uploadAttachmentAllowedFileTypes || null

    if (typeof maxSize === "string") maxSize = bytes(maxSize)
    if (maxSize <= 0) {
        console.error(new Error('maxSize设置参数不正确'))
        return;
    }
    // 最终的配置 -----------------------------------------
    return {
        host: "",
        api: server,
        meta: {...params},
        maxFileSize: maxSize,
        minFileSize: minSize,
        multiple: maxLength > 1,
        maxNumberOfFiles: maxLength,
        minNumberOfFiles: minLength,
        allowedFileTypes: allowedFileTypes,
        xhr: {
            headers: headers,
            fileName: fileName,
        }
    }
}
